import pandas as pd
import datetime
from quant_researcher.quant.project_tool.time_tool import timestamp_to_str
# timestamp_to_str
import json
import pytz
# from datetime import datetime


def timestamp_to_beijing(timestamp, timestamp_unit='s'):
    """
    将时间戳转换为北京时间

    参数:
    timestamp: 时间戳
    timestamp_unit: 时间戳单位 's'=秒, 'ms'=毫秒
    """
    # 处理时间戳单位
    if timestamp_unit == 'ms':
        timestamp = timestamp / 1000  # 毫秒转秒

    # 创建UTC时间对象
    utc_time = datetime.datetime.utcfromtimestamp(timestamp)

    # 定义时区
    utc_zone = pytz.timezone('UTC')
    beijing_zone = pytz.timezone('Asia/Shanghai')

    # 添加时区信息并转换
    utc_time = utc_zone.localize(utc_time)
    beijing_time = utc_time.astimezone(beijing_zone)

    return beijing_time


path = "./json/FuturesCmeVolumeDailySum.json"

with open(path, "r") as f:
    data = json.load(f)
data_df = pd.DataFrame(data)
# data_df['datetime'] = data_df['t'].apply(lambda x: datetime.datetime.fromtimestamp(x))
# data_df['time2'] =  data_df['t'].apply(lambda x: timestamp_to_str(x))
data_df['datetime'] = data_df['t'].apply(lambda x: timestamp_to_beijing(x))
data_df.to_csv("./data/FuturesCmeVolumeDailySum.csv", index=False)